package com.LeeCode;

/**
 * 爱生气的书店老板
 */

public class Code1052 {
    public static void main(String[] args) {
        int[] customers = {1, 0, 1, 2, 1, 1, 7, 5};
        int[] grumpy = {0, 1, 0, 1, 0, 1, 0, 1};
        int minutes = 3;
        System.out.println(new Code1052().maxSatisfied(customers, grumpy, minutes));
    }

    public int maxSatisfied(int[] customers, int[] grumpy, int minutes) {
        int ans = 0, cur = 0;
        for (int i = 0; i < minutes; i++) {
            cur += customers[i];
        }
        for (int i = minutes; i < grumpy.length; i++) {
            cur += (1 - grumpy[i]) * customers[i];
        }

        ans = Math.max(ans, cur);

        for (int i = minutes; i < grumpy.length; i++) {
            cur += grumpy[i] * customers[i] - grumpy[i - minutes] * customers[i - minutes];
            ans = Math.max(ans, cur);
        }

        return ans;
    }

    public int maxSatisfied1(int[] customers, int[] grumpy, int minutes) {
        int[] s = new int[2];
        int maxS1 = 0;
        for (int i = 0; i < customers.length; i++) {
            s[grumpy[i]] += customers[i];
            if (i < minutes - 1) { // 窗口长度不足 minutes
                continue;
            }
            maxS1 = Math.max(maxS1, s[1]);
            // 窗口最左边元素离开窗口
            s[1] -= grumpy[i - minutes + 1] > 0 ? customers[i - minutes + 1] : 0;
        }
        return s[0] + maxS1;
    }
}
